package com.gakj.truck.third.api.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gakj.truck.third.api.mapper.CustomorMapper;
import com.gakj.truck.third.api.mapper.TruckOumanMapper;
import com.gakj.truck.third.core.domain.*;
import com.gakj.truck.third.core.domain.area.ShtCity;
import com.gakj.truck.third.core.domain.area.ShtDistrict;
import com.gakj.truck.third.core.domain.area.ShtProvince;
import com.gakj.truck.third.core.enums.*;
import com.gakj.truck.third.core.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by gc on 2016/8/1.
 */
@Service
@Transactional
public class HuaTongService {

    private static final String NOT_SAVE = "0";
    private static final String HAS_SAVE = "1";

    private static final String IMG_PREFIX = "http://www.2scv.cn";

    private static final Logger LOGGER = LoggerFactory.getLogger(HuaTongService.class);

    @Autowired
    TruckOumanMapper oumanMapper;

    @Autowired
    CustomorMapper customorMapper;

    @Autowired
    UserDtoService userService;

    @Autowired
    AreaService areaService;

    @Autowired
    TruckBrandService brandService;

    @Autowired
    TruckModelService modelService;

    @Autowired
    TruckSeriesService seriesService;

    @Autowired
    TruckImgDtoService imgDtoService;

    @Autowired
    TruckService truckService;

    public JSONObject saveOrUpdate(TruckOuman ouman) {
        JSONObject result = new JSONObject();

        Long truckid = ouman.getTruckId();

        TruckOuman truckOuman = oumanMapper.getByOumanid(truckid);
        if (truckOuman != null) {
            //update
            TruckOuman properties = copyProperties(ouman, truckOuman);
            try {

                oumanMapper.update(properties);
            } catch (Exception ex) {
                ex.printStackTrace();
                return RequestUtils.failure("修改车辆信息失败!" + ex.getMessage());
            }
            try {
                updateTruckOfSnail(properties);
                return RequestUtils.success("");
            } catch (Exception e) {
                e.printStackTrace();
                return RequestUtils.failure("更新到蜗牛数据库失败!" + e.getMessage());
            }


        } else {
            //create
            ouman.setEnterupdatetime(new Date());
            ouman.setEntercreatetime(new Date());
            ouman.setHasSave(NOT_SAVE);
            oumanMapper.save(ouman);

            try {
                saveTruckOfSnail(ouman);
                return RequestUtils.success("");
            } catch (Exception e) {
                e.printStackTrace();
                return RequestUtils.failure("保存到蜗牛数据库失败!" + e.getMessage());
            }
        }
    }

    public void saveTruckOfSnail(TruckOuman ouman) throws Exception {
        Truck truck = transformToTruck(ouman, "save");
        truck.setUpdatetime(new Date());
        truck.setTmptime(new Date());
        truck.setProxysell("0");
        truck.setNeedcheck("0");
        truck.setJcflag("0");
        truck.setCarstatus(EnumTruckPublishStatus.WAITAUDIT.getCode());
        Customor customor = customorMapper.getCustomorByid(truck.getCreateuserid());
        if (customor != null && customor.getIsjoin().equals("1")) {
            truck.setSource("1");
        }
        truck.setDeltag(EnumIsDelete.NOTDELETE.getCode());

        truckService.save(truck);

        saveInfoid(truck, ouman);
        saveTruckImages(ouman.getImgList(), ouman.getSource(), truck);

        Long id = truck.getId();
        ouman.setRelationId(id);
        ouman.setHasSave(HAS_SAVE);
        ouman.setEnterupdatetime(new Date());
        oumanMapper.update(ouman);
    }

    public void updateTruckOfSnail(TruckOuman ouman) throws Exception {

        Truck truck = transformToTruck(ouman, "update");
        truck.setUpdatetime(new Date());
        truckService.update(truck);

    }

    public Truck transformToTruck(TruckOuman ouman, String flag) throws Exception {
        LOGGER.info("HuaTongService[[ transformToTruck  ]] 转换成truck对象开始..........");
        LOGGER.info("flag === " + flag);

        Truck dto = null;

        if (flag.equals("save")) {

            LOGGER.info("保存!");
            dto = new Truck();
        } else if (flag.equals("update")) {
            LOGGER.info("修改!");

            dto = truckService.get(ouman.getRelationId());
        }

        LOGGER.info("欧曼对象=======>>>>" + JSONObject.toJSONString(ouman));

        String contacttel = ouman.getContacttel();
        String contactname = ouman.getContactname();
        String carstatus = ouman.getCarstatus();
        String color = ouman.getColor();
        String emission = ouman.getEmission();
        String firsthand = ouman.getFirsthand();
        String viewcount = ouman.getViewcount();
        String power = ouman.getPower();
        String ownerdepict = ouman.getOwnerdepict();
        String title = ouman.getTitle();
        String showmileage = ouman.getShowmileage();
        String seecarplace = ouman.getSeecarplace();
        Float price = ouman.getPrice();
        String vin = ouman.getVin();
        String driveform = ouman.getDriveform();
        String drivercard = ouman.getDrivercard();
        String licence = ouman.getLicence();
        String oilType = ouman.getOilType();
        String motorCode = ouman.getMotorCode();

        String source = ouman.getSource();
        StringBuilder sb = new StringBuilder();
        if (dto != null && !source.equals("1")) {
            Date checktime = formatDate(ouman.getChecktime());
            if (checktime != null) {
                dto.setInspectiontime(DateUtil.formatDateToString(checktime, "yyyy-MM"));
            }
            Date createtime = formatDate(ouman.getCreatetime());
            dto.setCreatetime(createtime);
            Date cardtime = formatDate(ouman.getCardtime());
            dto.setCardtime(cardtime);
            if (cardtime != null) {
                sb.append(cardtime);
            }
            String cityName = ouman.getCityName();
            String areaname = ouman.getAreaname();
            String provinceName = ouman.getProvinceName();

            ShtCity shtCity = areaService.getShtCityByname(cityName);
            ShtProvince shtProvince = areaService.getProvinceByname(provinceName);
            ShtDistrict shtDistrict = areaService.getShtDistrictByname(areaname, shtCity.getId(), shtProvince.getId());

            if (shtCity != null) {
                dto.setCity(shtCity.getId());
                dto.setSeecityid(shtCity.getId());
            }
            if (shtProvince != null) {
                dto.setProvinces(shtProvince.getId());
                dto.setSeeprovince(shtProvince.getId());
            }
            if (shtDistrict != null) {
                dto.setAreaid(shtDistrict.getId());
                dto.setDistricts(shtDistrict.getId());
            }
            dto.setSeecarplace(seecarplace);
            dto.setDrivercard(drivercard);
            dto.setShowmileage(Float.valueOf(showmileage));
            if (StringUtils.isNotBlank(showmileage)) {
                sb.append(showmileage);
            }
        } else if (source.equals("1")) {
            dto.setProvinces(15);
            dto.setCity(142);
            dto.setDistricts(1354);

            /**
             * 坑啊,大坑啊@你妹
             */
            dto.setSeeprovince(15);
            dto.setSeecityid(142);
            dto.setAreaid(1354);

            dto.setSeecarplace("拳铺镇梁济公路西侧168号华通二手车市场");
            price = price / 10000;
            JSONArray array = JSONArray.parseArray(drivercard);
            if (array != null && array.size() > 0) {
                JSONObject jsonObject = array.getJSONObject(0);
                dto.setDrivercard(jsonObject.getString("fileName"));
            }
            Date checktime = dealHuatongDateString(ouman.getChecktime());
            LOGGER.info("*****************************************************************************************");
            LOGGER.info("转换检测日期,欧曼的检测日期 ===>>>" + ouman.getChecktime());
            LOGGER.info("转换之后存入蜗牛数据库的检测日期格式化为===>>" + DateUtil.getDefaultDateFormatString(checktime));
            LOGGER.info("*****************************************************************************************");
            /**
             * checktime ===>>>> 检测时间
             *  inspectiontime ===>>> 年检日期
             */
//            dto.setChecktime(checktime);
            if (checktime != null) {
                dto.setInspectiontime(DateUtil.formatDateToString(checktime, "yyyy-MM"));
            }
            Date createtime = dealHuatongDateString(ouman.getCreatetime());
            dto.setCreatetime(createtime);
            Date cardtime = dealHuatongDateString(ouman.getCardtime());
            dto.setCardtime(cardtime);
            dto.setShowmileage(Float.valueOf(showmileage) / 10000);
            if (cardtime != null) {
                sb.append(DateUtil.formatDateToString(cardtime, "yyyy"));
            }
            if (StringUtils.isNotBlank(showmileage)) {
                sb.append(Float.valueOf(showmileage) / 10000);
            }
        }
        String brandName = ouman.getBrandName();
        String modelName = ouman.getModelName();
        String seriesName = ouman.getSeriesName();
        List<TruckBrand> brands = brandService.getByName(brandName);
        List<TruckModel> models = modelService.getByName(modelName);
        List<TruckSeries> series = seriesService.getSeriesByName(seriesName);
        Long orRegister = getOrRegister(contacttel);
        dto.setCreateuserid(orRegister);

        dto.setVin(vin);
        dto.setContacttel(contacttel);
        dto.setContacttname(contactname);
        dto.setColor(color);
        dto.setPrice(price);
        dto.setViewcount(StringUtils.isNotBlank(viewcount) && !viewcount.equals("null") ? Integer.valueOf(viewcount) : 0);
        dto.setTitle(title);
        dto.setEmission(emission);
        dto.setFirsthand("1");
        dto.setPower(power);
        //通过华通导入的车辆描述默认为空
        dto.setOwnerdepict("");
        if (brands != null && brands.size() > 0) {
            dto.setBrandid(brands.get(0).getId());
        }
        if (models != null && models.size() > 0) {
            dto.setModel(models.get(0).getId());
        }
        if (series != null && series.size() > 0) {
            dto.setSeriesid(series.get(0).getId());
        }

        if (StringUtils.isNotBlank(brandName)) {
            sb.append(brandName);
        }
        if (StringUtils.isNotBlank(modelName)) {
            sb.append(modelName);
        }

        if (price != null) {
            sb.append(price + "");
        }

        dto.setDriveform(driveform);

        dto.setLicense(licence);
        dto.setKeyword(sb.toString());
        dto.setPlatform(getPlatform(source));
        dto.setFuel(StringUtils.isNotBlank(oilType) ? Integer.valueOf(oilType) : 0);
        LOGGER.info("HuaTongService[[ transformToTruck  ]] 转换成truck对象结束..........");
        return dto;
    }

    private TruckOuman copyProperties(TruckOuman source, TruckOuman target) {
        target.setImgList(source.getImgList());
        target.setModelName(source.getModelName());
        target.setSeriesName(source.getSeriesName());
        target.setBrandName(source.getBrandName());
        target.setPrice(source.getPrice());
        target.setShowmileage(source.getShowmileage());
        target.setEmission(source.getEmission());
        target.setPower(source.getPower());
        target.setDriveform(source.getDriveform());
        target.setFirsthand(source.getFirsthand());
        target.setColor(source.getColor());
        target.setContacttel(source.getContacttel());
        target.setContactname(source.getContactname());
        target.setSeecarplace(source.getSeecarplace());
        target.setDrivercard(source.getDrivercard());
        target.setOwnerdepict(source.getOwnerdepict());
        target.setCreatetime(source.getCreatetime());
        target.setLicence(source.getLicence());
        target.setVin(source.getVin());
        target.setCardtime(source.getCardtime());
        target.setChecktime(source.getChecktime());
        target.setSource(source.getSource());
        target.setTruckId(source.getTruckId());
        target.setTitle(source.getTitle());
        target.setProvinceName(source.getProvinceName());
        target.setCityName(source.getCityName());
        target.setAreaname(source.getAreaname());

        return target;
    }

    private Long getOrRegister(String contacttel) {
        Long userid;
        // 根据contacttel查询 用户id
        Customor customor = customorMapper.getCustomorByUserTell(contacttel);
        if (customor != null) {
            userid = customor.getId();
        } else {
            UserDto newUser = new UserDto();
            newUser.setLoginName(contacttel);
            newUser.setName(contacttel);
            newUser.setMobile(contacttel);
            newUser.setDeltag(EnumIsDelete.NOTDELETE.getCode()); // 默认是未删除
            newUser.setIsanon(EnumIsAnon.ANON.getCode()); // 默认是匿名
            newUser.setPassword(contacttel.substring(5));
            userService.registerUserPwd(newUser);

            Customor newCustomor = new Customor();
            newCustomor.setUserid(newUser.getId());
            newCustomor.setIsjoin(EnumIsJoin.NOTJOIN.getCode()); // 默认注册用户不是加盟商
            newCustomor.setStatus(EnumIsFrozen.NOTFROZEN.getCode()); // 默认注册用户不会冻结
            newCustomor.setAuditstatus(EnumAuditStatus.CUSTOMOR.getCode()); // 表示是普通用户
            newCustomor.setIntegration(50);
            newCustomor.setLotterys(0);
            newCustomor.setReward(0);
            newCustomor.setInvitenum(0);
            newCustomor.setInvitetype("0");
            newCustomor.setIsComing(EnumUserResource.HUATONG.getCode());
            customorMapper.saveCustomor(newCustomor);
            userid = newCustomor.getId();
            SMSDYSender.sendSms(contacttel);
        }
        return userid;
    }

    private Date formatDate(String datestr) throws ParseException {
        return StringUtils.isNotBlank(datestr) ? DateUtils.parseDate(datestr, "yyyy-MM-dd HH:mm:ss") : null;
    }

    private void saveTruckImages(String imgList, String source, Truck tk) {
        if (StringUtils.isNotBlank(imgList)) {
            //设置图片表里的deltag=1,已删除
            imgDtoService.deleteByTruckid(tk.getId());
            JSONArray array = JSONArray.parseArray(imgList);
            List<TruckImgDto> imgDtos = new ArrayList<>();
            for (int i = 0; i < array.size(); i++) {
                Object anArray = array.get(i);
                JSONObject object = (JSONObject) anArray;
                TruckImgDto truckImgDto = new TruckImgDto();
                //[{"fileName":"/upload/upload/20160322/c0927b34-509a-48cb-a77e-daa126fa4a1d.jpg","originalName":"1.jpg"},{"fileName":"/upload/upload/20160322/2f3692c6-818f-4349-9d22-3656843f8abc.jpg","originalName":"封面同步图片"}]
                String location = "";
                if (source.equals("0")) {
                    location = IMG_PREFIX + object.getString("fileName");
                } else if (source.equals("1")) {
                    location = object.getString("fileName");
                }
                String name = object.getString("originalName");
                truckImgDto.setCid(tk.getId());
                truckImgDto.setDeltag("0");
                truckImgDto.setImgpath(location);
                truckImgDto.setLocation("0");
                truckImgDto.setCreateDate(new Date());
                if (i == 0) {
                    truckImgDto.setIsthumbnail("1");
                } else {
                    truckImgDto.setIsthumbnail("0");
                }
                imgDtos.add(truckImgDto);
            }

            if (imgDtos.size() > 0) {
                try {
                    imgDtoService.batchSave(imgDtos);
                    LOGGER.info("图片信息更新成功");
                } catch (Exception ex) {
                    ex.printStackTrace();
                    LOGGER.error(ex.getMessage());
                }
            }
        }
    }

    private void saveInfoid(Truck tk, TruckOuman ouman) {
        String infoid = "";
        String keyword = tk.getKeyword();
        if (ouman.getSource().equals("1")) {
            infoid = buildInfoid(tk);
        } else {
            infoid = DateUtil.formatDateToString(tk.getCreatetime(), "yyMMdd") + tk.getId().toString().substring(tk.getId().toString().length() - 4, tk.getId().toString().length());
        }
        tk.setKeyword(infoid + keyword);
        tk.setInfoid(infoid);
        truckService.update(tk);
    }

    private String buildInfoid(Truck tk) {
        String infoid = DateUtil.formatDateToString(tk.getCreatetime(), "yyyyMM") + CdkeyUtil.generateCdkey(4, CdkeyUtil.NUMBER);
        Truck byinfoid = truckService.findTruckByinfoid(infoid);
        return byinfoid == null ? infoid : buildInfoid(tk);
    }

    private String getPlatform(String source) {
        switch (source) {
            case "0":
                return EnumPlatform.DATA_OUMAN.getCode();
            case "1":
                return EnumPlatform.SOURCE_HUATONG.getCode();
            default:
                return "";
        }
    }

    private String getDriveform(String driveform) {
        if (StringUtils.isNotBlank(driveform)) {
            if (driveform.indexOf("*") != -1) {
                driveform.replace("*", "x");
            }
            switch (driveform) {
                case "4x2":
                    driveform = "1";
                    break;
                case "6x2":
                    driveform = "2";
                    break;
                case "6x4":
                    driveform = "3";
                    break;
                case "6x6":
                    driveform = "4";
                    break;
                case "8x2":
                    driveform = "5";
                    break;
                case "8x4":
                    driveform = "6";
                    break;
                default:
                    driveform = "";
                    break;
            }
        } else {
            driveform = "";
        }
        return driveform;
    }

    private String getEmission(String emission) {
        switch (emission) {
            case "国三":
                emission = "1";
                break;
            case "国四":
                emission = "2";
                break;
            case "国二":
                emission = "3";
                break;
            case "国五":
                emission = "4";
                break;
            case "国3":
                emission = "1";
                break;
            case "国4":
                emission = "2";
                break;
            case "国2":
                emission = "3";
                break;
            case "国5":
                emission = "4";
                break;
            default:
                emission = "";
                break;
        }
        return emission;
    }

    private static Date dealHuatongDateString(String dateString) {
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }
        if (dateString.contains("/")) {
            dateString = dateString.replaceAll("/", "-");
        }
        String replaceAll = dateString + " 00:00:00";
        return DateUtil.defaultFormatStringToDate(replaceAll);
    }

    public static void main(String[] args) {
        String date = "2017/11/8";
        Date date1 = dealHuatongDateString(date);
        System.out.println(JSON.toJSONString(date1));
    }

    /********************************************
     * 新的华通接口(多了很多属性处理,主要是处理华通处理过来的新参数)
     **********************************************************************************/


    public JSONObject newSaveOrUpdate(Map map) {
        TruckOuman truckOuman = requestMap2TruckOuman(map);

        if (truckOuman.getTruckId().equals("0") || StringUtils.isEmpty(truckOuman.getTruckId() + "")) {
            return RequestUtils.failure("id不能位空且不能为0!");
        }
        if (StringUtils.isEmpty(truckOuman.getImgList())) {
            return RequestUtils.failure("车辆图片传输错误");
        }
        if (StringUtils.isEmpty(truckOuman.getModelName())) {
            return RequestUtils.failure("车型参数传递错误!");
        }
        if (StringUtils.isEmpty(truckOuman.getSeriesName())) {
            return RequestUtils.failure("车系参数错误");
        }
        if (StringUtils.isEmpty(truckOuman.getBrandName())) {
            return RequestUtils.failure("品牌参数错误!");
        }
        if (truckOuman.getPrice() == null || StringUtils.isEmpty(truckOuman.getPrice().toString()) || truckOuman.getPrice() == 0) {
            return RequestUtils.failure("价格不能为空并且不能为0");
        }

        if (StringUtils.isEmpty(truckOuman.getShowmileage()) || Integer.parseInt(truckOuman.getShowmileage()) == 0) {
            return RequestUtils.failure("表显里程不能位空且不能为0!");
        }

        if (StringUtils.isEmpty(truckOuman.getEmission())) {
            return RequestUtils.failure("排放标准不能为空!");
        }
        if (StringUtils.isEmpty(truckOuman.getPower()) || Integer.parseInt(truckOuman.getPower()) == 0) {
            return RequestUtils.failure("马力不能为空且不能为0");
        }

        if (StringUtils.isEmpty(truckOuman.getDriveform())) {
            return RequestUtils.failure("驱动形式不能为空!");
        }

        if (StringUtils.isEmpty(truckOuman.getColor())) {
            return RequestUtils.failure("颜色不能为空!");
        }

        if (StringUtils.isEmpty(truckOuman.getContactname())) {
            return RequestUtils.failure("联系人不能为空!");
        }

        if (StringUtils.isEmpty(truckOuman.getContacttel())) {
            return RequestUtils.failure("联系电话不能为空!");
        }

        if (StringUtils.isEmpty(truckOuman.getCreatetime())) {
            return RequestUtils.failure("createtime不能为空!");
        }

        if (StringUtils.isEmpty(truckOuman.getVin())) {
            return RequestUtils.failure("VIN码不能位空!");
        }

        if (StringUtils.isEmpty(truckOuman.getCardtime())) {
            return RequestUtils.failure("cardtime不能为空!");
        }

        if (StringUtils.isNotBlank(truckOuman.getLicence()) && !CheckUtils.checkLicence(truckOuman.getLicence())) {
            return RequestUtils.failure("车牌号码格式不正确,如：鲁HN5675");
        }

        return saveOrUpdate(truckOuman);
    }

    private TruckOuman requestMap2TruckOuman(Map map) {

        Long truckId = Long.valueOf(map.get("id").toString());
        String title = String.valueOf(map.get("title"));
        String color = String.valueOf(map.get("color"));
        String cardtime = String.valueOf(map.get("cardtime"));
        Float price = Float.valueOf(map.get("price").toString());
        String createtime = String.valueOf(map.get("createtime"));
        String carstatus = String.valueOf(map.get("carstatus"));
        String power = String.valueOf(map.get("power"));
        String updatetime = String.valueOf(map.get("updatetime"));
        String showmileage = String.valueOf(map.get("showmileage"));
        String ownerdepict = String.valueOf(map.get("ownerdepict"));
        String viewcount = String.valueOf(map.get("viewcount"));
        String firsthand = String.valueOf(map.get("firsthand"));
        String vin = String.valueOf(map.get("vin"));
        String contactname = String.valueOf(map.get("contactname"));
        String contacttel = String.valueOf(map.get("contacttel"));
        String seecarplace = String.valueOf(map.get("seecarplace"));
        String emission = String.valueOf(map.get("emission"));
        String areaname = String.valueOf(map.get("areaname"));
        String username = String.valueOf(map.get("username"));
        String cityName = String.valueOf(map.get("cityName"));
        String brandName = String.valueOf(map.get("brandName"));
        String provinceName = String.valueOf(map.get("provinceName"));
        String imgThumbnail = String.valueOf(map.get("imgThumbnail"));
        String userType = String.valueOf(map.get("userType"));
        String imgList = String.valueOf(map.get("imgList"));
        String modelName = String.valueOf(map.get("modelName"));
        String seriesName = String.valueOf(map.get("seriesName"));
        String driveform = String.valueOf(map.get("driveform"));
        String drivercard = String.valueOf(map.get("drivercard"));
        String licence = String.valueOf(map.get("licence"));
        String checktime = String.valueOf(map.get("checktime"));


        String expireDateEnd = String.valueOf(map.get("expireDateEnd"));
        String expireDateStart = String.valueOf(map.get("expireDateStart"));
        String motorCode = String.valueOf(map.get("motorCode"));
        String ownerAddress = String.valueOf(map.get("ownerAddress"));
        String ownerCardNumber = String.valueOf(map.get("ownerCardNumber"));
        String ownerAgent = String.valueOf(map.get("ownerAgent"));
        String ownerAgentContacttel = String.valueOf(map.get("ownerAgentContacttel"));
        String buyerName = String.valueOf(map.get("buyerName"));
        String buyerCardNumber = String.valueOf(map.get("buyerCardNumber"));
        String buyerContacttel = String.valueOf(map.get("buyerContacttel"));
        String buyerContactAddress = String.valueOf(map.get("buyerContactAddress"));
        String dealDate = String.valueOf(map.get("dealDate"));
        String selfPrice = String.valueOf(map.get("selfPrice"));
        String downPayment = String.valueOf(map.get("downPayment"));
        String carLength = String.valueOf(map.get("carLength"));
        String carHeight = String.valueOf(map.get("carHeight"));
        String carWidth = String.valueOf(map.get("carWidth"));
        String quality = String.valueOf(map.get("quality"));
        String normalPayload = String.valueOf(map.get("normalPayload"));
        String boxType = String.valueOf(map.get("boxType"));
        String motorSerial = String.valueOf(map.get("motorSerial"));
        String oilType = String.valueOf(map.get("oilType"));
        String gearboxType = String.valueOf(map.get("gearboxType"));
        String shiftMode = String.valueOf(map.get("shiftMode"));
        String gearsNumber = String.valueOf(map.get("gearsNumber"));
        String frontAxle = String.valueOf(map.get("frontAxle"));
        String backAxle = String.valueOf(map.get("backAxle"));
        String suspensionType = String.valueOf(map.get("suspensionType"));

        String source = String.valueOf(map.get("source"));


        TruckOuman entity = new TruckOuman();

        entity.setTruckId(truckId);
        entity.setTitle(getDefaultValue(title));
        entity.setColor(getDefaultValue(color));
        entity.setCardtime(getDefaultValue(cardtime));
        entity.setPrice(price != null ? price : 0.00f);
        entity.setCreatetime(getDefaultValue(createtime));
        entity.setPower(getDefaultValue(power));
        entity.setUpdatetime(getDefaultValue(updatetime));
        entity.setShowmileage(getDefaultValue(showmileage));
        entity.setOwnerdepict(getDefaultValue(ownerdepict));
        entity.setViewcount(getDefaultValue(viewcount));
        entity.setFirsthand(getDefaultValue(firsthand));
        entity.setVin(getDefaultValue(vin));
        entity.setContactname(getDefaultValue(contactname));
        entity.setContacttel(getDefaultValue(contacttel));
        entity.setSeecarplace(getDefaultValue(seecarplace));
        entity.setEmission(getDefaultValue(emission));
        entity.setAreaname(getDefaultValue(areaname));
        entity.setUsername(getDefaultValue(username));
        entity.setCityName(getDefaultValue(cityName));
        entity.setBrandName(getDefaultValue(brandName));
        entity.setProvinceName(getDefaultValue(provinceName));
        entity.setImgThumbnail(getDefaultValue(imgThumbnail));
        entity.setUserType(getDefaultValue(userType));
        entity.setImgList(getDefaultValue(imgList));
        entity.setModelName(getDefaultValue(modelName));
        entity.setSeriesName(getDefaultValue(seriesName));
        entity.setDriveform(getDefaultValue(driveform));
        entity.setDrivercard(getDefaultValue(drivercard));
        entity.setLicence(getDefaultValue(licence));
        entity.setChecktime(getDefaultValue(checktime));

        entity.setEntercreatetime(new Date());
        entity.setEnterupdatetime(new Date());
        entity.setHasSave("0");
        entity.setSource(getDefaultValue(source));

        entity.setExpireDateEnd(getDefaultValue(expireDateEnd));
        entity.setExpireDateStart(getDefaultValue(expireDateStart));
        entity.setMotorCode(getDefaultValue(motorCode));
        entity.setOwnerAddress(getDefaultValue(ownerAddress));
        entity.setOwnerCardNumber(getDefaultValue(ownerCardNumber));
        entity.setOwnerAgent(getDefaultValue(ownerAgent));
        entity.setOwnerAgentContacttel(getDefaultValue(ownerAgentContacttel));
        entity.setBuyerName(getDefaultValue(buyerName));
        entity.setBuyerCardNumber(getDefaultValue(buyerCardNumber));
        entity.setBuyerContacttel(getDefaultValue(buyerContacttel));
        entity.setBuyerContactAddress(getDefaultValue(buyerContactAddress));
        entity.setDealDate(getDefaultValue(dealDate));
        entity.setSelfPrice(getDefaultValue(selfPrice));
        entity.setDownPayment(getDefaultValue(downPayment));
        entity.setCarLength(getDefaultValue(carLength));
        entity.setCarHeight(getDefaultValue(carHeight));
        entity.setCarWidth(getDefaultValue(carWidth));
        entity.setQuality(getDefaultValue(quality));
        entity.setNormalPayload(getDefaultValue(normalPayload));
        entity.setBoxType(getDefaultValue(boxType));
        entity.setMotorSerial(getDefaultValue(motorSerial));
        entity.setOilType(getDefaultValue(oilType));
        entity.setGearboxType(getDefaultValue(gearboxType));
        entity.setShiftMode(getDefaultValue(shiftMode));
        entity.setGearsNumber(getDefaultValue(gearsNumber));
        entity.setFrontAxle(getDefaultValue(frontAxle));
        entity.setBackAxle(getDefaultValue(backAxle));
        entity.setSuspensionType(getDefaultValue(suspensionType));


        return entity;
    }

    private static String getDefaultValue(String str) {
        return StringUtils.isNotBlank(str) ? str : "";

    }

}
